<?php

include_once 'baseInformatica.php';

db_connect();

function db_connect() {
    $conn = @mysqli_connect(HOST, USER, PASSWD, DB, PORT) or die_db_link();
    $charset = 'utf8';
    mysqli_set_charset($conn, $charset) or die_db_error($conn, '字符集' . $charset . '设置失败');
    return $conn;
}

function die_db_link() {
    $args = func_get_args();
    $num = count($args);
    $msg = $num === 0 ? '数据库连接失败' : $args[0];
    die(to_json(array('status' => $msg)));
}

function die_db_error($conn) {
    $args = func_get_args();
    $num = count($args);
    $msg = mysqli_error($conn);
    if ($num > 1) {
        $msg = $args[1];
    }
    mysqli_close($conn);
    die(to_json(array('status' => $msg)));
}

function to_json($obj) {
    return json_encode($obj, JSON_UNESCAPED_UNICODE);
}
